
var express = require('express');
var router = express.Router();
var models = require('../models');
var uuid = require("uuid");
var logger = require("../middleware/logger");
var needauth = require("../middleware/needauth");
var noted = require('../services/noted');
var fs = require("fs");
var path = require("path");
var Sequelize = require("sequelize");
var env = process.env.NODE_ENV || "development";
var config = require(path.join(__dirname, '..', 'config', 'config-sequelize'))[env];
var sequelize = new Sequelize(config.database, config.username, config.password, config);
router.post('/create', function (req, res) {
    var _obj = {
        noteid: uuid.v1().replace(/-/g,""),
        nickname: req.pack.req.data.nickname||"新建笔记",
        userid: req.pack.req.data.userid,
        type: req.pack.req.data.type || 0,
        limit: 1,
        content: req.pack.req.data.content || "",
        ext: req.pack.req.data.ext || null
    };
    if(req.pack.req.data.picture){
        _obj.picture=req.pack.req.data.picture
    }
    if(req.pack.req.data.css ){
        _obj.css=req.pack.req.data.css
    }
    if(req.pack.req.data.is_pass){
        _obj.is_pass=req.pack.req.data.is_pass
        _obj.pass=req.pack.req.data.pass
    }
    noted.create(_obj)
        .then(function (data) {
            req.pack.setMsg({type: 1, data: '创建成功'});
            req.pack.setResult(data);
            res.send(req.pack);
        })
        .catch(function (err) {
            req.pack.setStatus(-1);
            req.pack.setMsg({type: 2, data: err.message});
            res.send(req.pack);
        });
});

router.post('/delete', function (req, res) {
    if (req.pack.auth.power == 0) {
        noted.delete(req.pack.req.data.noteid)
            .then(function () {
                req.pack.setMsg({type: 1, data: '删除成功'});
                res.send(req.pack);

            })
            .catch(function (err) {
                req.pack.setStatus(-1);
                req.pack.setMsg({type: 2, data: err.message});
                res.send(req.pack);
            });
    } else {
        req.pack.setStatus(2);
        req.pack.setMsg({type: 4, data: '没有操作权限,请联系超级管理员'});
        res.send(req.pack);
    }
});

router.post('/update', function (req, res) {
    var _obj = {
        noteid: req.pack.req.data.noteid,
        nickname: req.pack.req.data.nickname || "",
        content: req.pack.req.data.content || "",
    };
    noted.update(_obj)
        .then(function () {
            req.pack.setMsg({type: 1, data: '更新成功'});
            res.send(req.pack);
        })
        .catch(function (err) {
            req.pack.setStatus(-1);
            req.pack.setMsg({type: 2, data: err.message});
            res.send(req.pack);
        });
});


router.post('/search', function (req, res) {
    var key = req.pack.req.data.key;
    var value = '%' + req.pack.req.data.value + '%';
    var st = req.pack.req.data.startDatetime || null;
    var et = req.pack.req.data.endDatetime || null;

    var obj = {};
    var obj2 = {};
    obj2['$like'] = value;
    obj[key] = obj2;

    if (st && et) {
        obj2['$between'] = [st, et];
        obj['createdAt'] = obj2;
    }
    var userid =req.pack.req.data.userid || null;
    if(userid){
        obj["userid"] =userid;
    }
    models.noted.findAll({
            where: obj
        }
    ).then(function (data) {
        if (data) {
            req.pack.setResult(data);
            res.send(req.pack);
        } else {
            req.pack.setMsg({type: 4, data: '找不到相应产品信息'});
        }
    }).catch(function (err) {
        req.pack.setStatus(-1);
        req.pack.setMsg({type: 2, data: err.message});
        res.send(req.pack);
    });
});

router.post('/listPage', needauth, function (req, res) {

    var offset = parseInt(req.pack.req.data.offset);
    var limit = parseInt(req.pack.req.data.limit) || 20;
    var st = req.pack.req.data.startDatetime || null;
    var et = req.pack.req.data.endDatetime || null;

    noted.listPage(offset, limit, st, et)
        .then(function (data) {

            req.pack.setResult(data)
            res.send(req.pack);
        })
        .catch(function (err) {

            req.pack.setStatus(-1);
            req.pack.setMsg({type: 2, data: err.message});
            res.send(req.pack);
        });
});

router.post('/detail', function (req, res) {
    noted.findOne(req.pack.req.data.noteid)
        .then(function (result) {
            req.pack.setResult(result);
            res.send(req.pack);
        })
        .catch(function (err) {
            req.pack.setStatus(-1);
            req.pack.setMsg({type: 2, data: err.message});
            res.send(req.pack);
        });
});

router.post('/count', function (req, res) {
    var where = {userid:req.pack.req.data.userid};
    models.noted.count({where: where})
        .then(function (result) {
            req.pack.setResult(result);
            res.send(req.pack);
        })
        .catch(function (err) {
            req.pack.setStatus(-1);
            req.pack.setMsg({type: 2, data: err.message});
            res.send(req.pack);
        });
});

router.post('/countYue', function (req, res) {

    sequelize.query("SELECT SUM(CASE WHEN MONTH(s.createdAt) = 1 THEN s.limit ELSE 0 END) AS '1',"+
    "SUM(CASE WHEN MONTH(s.createdAt) = 2 THEN s.limit ELSE 0 END) AS '2',"+
      "SUM(CASE WHEN MONTH(s.createdAt) = 3 THEN s.limit ELSE 0 END) AS '3',"+
       " SUM(CASE WHEN MONTH(s.createdAt) = 4 THEN s.limit ELSE 0 END) AS '4',"+
        "SUM(CASE WHEN MONTH(s.createdAt) = 5 THEN s.limit ELSE 0 END) AS '5',"+
        "SUM(CASE WHEN MONTH(s.createdAt) = 6 THEN s.limit ELSE 0 END) AS '6',"+
        "SUM(CASE WHEN MONTH(s.createdAt) = 7 THEN s.limit ELSE 0 END) AS '7',"+
        "SUM(CASE WHEN MONTH(s.createdAt) = 8 THEN s.limit ELSE 0 END) AS '8',"+
        "SUM(CASE WHEN MONTH(s.createdAt) = 9 THEN s.limit ELSE 0 END) AS '9',"+
        "SUM(CASE WHEN MONTH(s.createdAt) = 10 THEN s.limit ELSE 0 END) AS '10',"+
        "SUM(CASE WHEN MONTH(s.createdAt) = 11 THEN s.limit ELSE 0 END) AS '11',"+
        "SUM(CASE WHEN MONTH(s.createdAt) = 12 THEN s.limit ELSE 0 END) AS '12'"+
    "FROM noteds AS s WHERE YEAR(s.createdAt) = '2017' "+ "and s.userid = '" +req.pack.req.data.userid+"'")
        .then(function (result) {
            req.pack.setResult(result);
            res.send(req.pack);
        })
        .catch(function (err) {
            req.pack.setStatus(-1);
            req.pack.setMsg({type: 2, data: err.message});
            res.send(req.pack);
        });
});
module.exports = router;